#ifndef REMOTECONTROL_HPP
#define REMOTECONTROL_HPP

#include <iostream>
#include <vector>
#include "Command.hpp"

class RemoteControl
{
public:
    RemoteControl(int num = 7)
        :_num(num)
    {
        // 初始化命令列表
        for(int i = 0; i < _num; i++)
        {
            _onCommands.push_back(nullptr);
            _offCommands.push_back(nullptr);
        }
        _prevCommad = nullptr;
    }

    // 设置命令
    void SetCommand(int solt, Command* on = nullptr, Command* off = nullptr)
    {
        _onCommands[solt] = on;
        _offCommands[solt] = off;
    }

    // 启动所有设置的命令
    void On()
    {
        for(int i = 0; i < _num; i++)
        {
            if(_onCommands[i] != nullptr)
                _onCommands[i]->Execute();
        }
    }

    void Off()
    {
        for(int i = 0; i < _num; i++)
        {
            if(_offCommands[i] != nullptr)
                _offCommands[i]->Execute();
        }
        _prevCommad = nullptr;
    }

    void OnButtonWasPushed(int solt)
    {
        if(_onCommands[solt] != nullptr)
            {
                _onCommands[solt]->Execute();
                _prevCommad = _onCommands[solt];
            }
        else std::cout << "The botton was unset!" << std::endl;
    }

    void OffButtonWasPushed(int solt)
    {
        if(_offCommands[solt] != nullptr)
        {
            _offCommands[solt]->Execute();
            _prevCommad = _offCommands[solt];
        }
        else std::cout << "The botton was unset!" << std::endl;
    }

    void UndoButonWasPushed()
    {
        if(_prevCommad != nullptr)
            _prevCommad->Undo();
    }
private:
    const int _num; // 远程遥控的按钮数量
    std::vector<Command*> _onCommands;
    std::vector<Command*> _offCommands;
    // 撤销命令
    Command* _prevCommad;
};

#endif
